//
//  SearchViewController.m
//  StandardizedProduct
//
//  Created by 宁宁 on 16/6/27.
//  Copyright © 2016年 ym. All rights reserved.
//

#import "SearchViewController.h"

#import "SearchHeaderView.h"//搜索View
#import "CategoryDetailViewController.h"

#import "GoodsModel.h"


@interface SearchViewController () <UITextFieldDelegate>

{
    NSMutableArray *_searchArray;

}

@property (weak, nonatomic) IBOutlet UIView *searchView;
@property (weak, nonatomic) IBOutlet UITextField *searchTextField;//搜索框

@end

@implementation SearchViewController


- (void)viewWillDisappear:(BOOL)animated{
    self.navigationController.navigationBar.hidden = NO;
}
- (void)viewWillAppear:(BOOL)animated{
    self.navigationController.navigationBar.hidden = YES;
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.searchTextField resignFirstResponder];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
 
    
    self.searchView.backgroundColor = [UIColor colorWithRed:255 green:255 blue:255 alpha:0.5];
    
    UIColor *color = [UIColor whiteColor];
    self.searchTextField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"输入关键字搜索" attributes:@{NSForegroundColorAttributeName: color}];
    _searchTextField.delegate = self;
    //设置键盘
//    [self setKeyBoard];
    
}
//搜索范围
- (IBAction)searchZone:(id)sender {
    NSLog(@"宝贝");
    if ([_searchTextField.text isEqualToString:@""]) {
        
        return;
    }
//    CategoryDetailViewController *cate = [[CategoryDetailViewController alloc] init];
//    NSString *encodingString = [_searchTextField.text stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
//    cate.hidesBottomBarWhenPushed = YES;
//    [self.navigationController pushViewController:cate animated:YES];
}
- (IBAction)cancel:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}
//- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
//{
//    
//    NSLog(@"阿拉啦啦啦-----%@",textField.text);
////   [self network:textField.text];
//    return YES;
//}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    if ([_searchTextField.text isEqualToString:@""]) {
        
        return YES;
    }
    NSString *encodingString = [_searchTextField.text stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    [self getSearch:encodingString];
    return YES;
}
- (void)getSearch:(NSString *)search{
    NSDictionary *paraments = @{
                  @"brand"           : search,
                  @"currentpage"     : @"1",
                  @"pagesize"        : @"32",
                  @"query"           : @"",
                  @"pricelow"        : @"0",
                  @"pricehigh"       : @"0",
                  @"marketPriceLow"  : @"0",
                  @"marketPriceHigh" : @"0",
                  
                  @"sortfield"       : @"all",
                  @"desc"            : @"true",
                  @"pw"              : @"-1"
                  };

    [NetWorkManager requestWithType:HttpRequestTypeGet withMethodName:@"12082/api/BOMProductExtend/SearchPorductByCondition?" withParaments:paraments withSuccessBlock:^(NSDictionary *object) {
        NSLog(@"-分类详情---- %@", object);
        if ([object[@"Code"] isEqualToString:@"0"]) {
            NSString *string = [NSString stringWithFormat:@"%@", object[@"TotalCount"]];
            if ([string isEqualToString:@"0"]) {
                [EBUtility prompt:@"没有您想要的商品" with:self];
            }else{
                if (isKindOfNSDictionary(object[@"Rows"])) {
                    _searchArray = [NSMutableArray array];
                    
                    NSDictionary *dict = object[@"Rows"];
                    if (isKindOfNSArray([dict objectForKey:@"product"])) {
                        NSArray *array = [dict objectForKey:@"product"];
                        if (array.count)
                        {
                            for (NSDictionary *smDic in array) {
                                GoodsModel *goods  = [GoodsModel parseUserWithDictionary:smDic];
                                
                                [_searchArray addObject:goods];
                            }
                            
                            CategoryDetailViewController *cate = [[CategoryDetailViewController alloc] init];
                            cate.searchArray = _searchArray;
                            cate.hidesBottomBarWhenPushed = YES;
                            [self.navigationController pushViewController:cate animated:YES];
                            
                        }
                    }
                }
            }
        }else{
            [EBUtility prompt:@"搜索失败" with:self];
        }
    } withFailureBlock:^(NSError *error) {
        NSLog(@"商品搜索网络请求失败 : %@", error);
    } progress:nil];

}

//网络请求
//- (void)network:(NSString *)tfString
//{
//
//
//
//
//
//        NSDictionary *paraments = @{
//                                    @"brand"           : encodingString,
//                                    @"currentpage"     : @"1",
//                                    @"pagesize"        : @"32",
//                                    };
//        
//        [NetWorkManager requestWithType:HttpRequestTypeGet withMethodName:@"12082/api/BOMProductExtend/SearchPorductByCondition?" withParaments:paraments withSuccessBlock:^(NSDictionary *object) {
//            NSLog(@"--搜索商品---- %@", object);
//            
//            if ([object[@"Message"] isEqualToString:@"null"]) {
//                return;
//            }else{
//                for (NSDictionary *dic in object[@"Rows"][@"product"]) {
//                    NSLog(@"rp---%@", dic[@"Title"]);
//                    
//                }
//                [_searchArray removeAllObjects];
//                for (NSDictionary *smDic in object[@"Rows"][@"product"]) {
//                    
//                    CartModel *classify  = [ClassifyModel parseUserWithDictionary:smDic];
//                    
//                    [_searchArray addObject:classify];
//                    
//                }
//                
//                for (NSDictionary *dic in object[@"Rows"][@"cat"]) {
//                    if ([dic[@"CateId"] isEqualToString:@"0"]) {
//                        break;
//                    }
//                    //                CateModel *model = [[CateModel alloc] init];
//                    //                [model setValuesForKeysWithDictionary:dic];
//                    //                [_categoryCollectionArray addObject:model];
//                }
//                
//                [_searchArray reloadData];
//                
//                            //默认选中第一个cell
//                            NSIndexPath *first = [NSIndexPath indexPathForRow:0 inSection:0];
//                            [_categoryTableView selectRowAtIndexPath:first animated:NO scrollPosition:UITableViewScrollPositionTop];
//            }
//            
//        } withFailureBlock:^(NSError *error) {
//            NSLog(@"商品搜索网络请求失败 : %@", error);
//        } progress:nil];
// 
//    
//}

//键盘通知
//- (void)setKeyBoard{
//    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyBoardWillChangeFrame:) name:UIKeyboardWillChangeFrameNotification object:nil];
//}
//- (void)keyboardWillChangeFrame:(NSNotification *)notification
//{
//    // 键盘显示\隐藏完毕的frame
//    CGRect frame = [notification.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
//    // 修改底部约束
//    self.bottomSapce.constant = HEIGHT - frame.origin.y;
//    // 动画时间
//    CGFloat duration = [notification.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
//    // 动画
//    [UIView animateWithDuration:duration animations:^{
//        [self.view layoutIfNeeded];
//    }];
//}
@end
